package pl.com.sthaether.mc.main;

import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.geometry.Insets;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import pl.com.sthaether.mc.config.MCMainConfiguration;

import java.text.NumberFormat;

public class MCMapInfoPane extends AnchorPane {

    private Label infoLabel;
//    private Label scaleLabel;
    private NumberFormat numberFormat;

    private DoubleProperty scaleDivider;

    private Slider scaleSlider;

    private MCMapInfoPane() {
        init();
    }

    private static class MCHolder{
        private static final MCMapInfoPane INSTANCE = new MCMapInfoPane();
    }

    public static MCMapInfoPane getInstance(){
        return MCHolder.INSTANCE;
    }

    private void init() {
        setPadding(new Insets(3, 5, 3, 5));

        scaleDivider = new SimpleDoubleProperty(5000L);

        numberFormat = NumberFormat.getNumberInstance();
        numberFormat.setMaximumFractionDigits(0);

        infoLabel = new Label("No map available");

//        scaleLabel = new Label("1:" + numberFormat.format(5000));

        scaleSlider = new Slider(MCMainConfiguration.MIN_SCALE_DIVIDER, MCMainConfiguration.MAX_SCALE_DIVIDER, MCMainConfiguration.MIN_SCALE_DIVIDER);
        scaleSlider.setMin(MCMainConfiguration.MIN_SCALE_DIVIDER);
        scaleSlider.setMax(MCMainConfiguration.MAX_SCALE_DIVIDER);
        scaleSlider.valueProperty().bindBidirectional(scaleDivider);
        scaleSlider.setSnapToTicks(true);
        scaleSlider.setPrefWidth(200);
//        scaleDivider.addListener((val) -> scaleLabel.setText("1:" + numberFormat.format(scaleDivider.getObjectValue())));

        HBox innerPane = new HBox();
        innerPane.setPadding(new Insets(0, 2, 0, 2));
//        innerPane.setLeftAnchor(scaleLabel, 1.0);
//        innerPane.setRightAnchor(scaleSlider, 1.0);
        setLeftAnchor(infoLabel, 3.0);
        setRightAnchor(innerPane, 1.0);

        setBottomAnchor(infoLabel, 3.0);
        setBottomAnchor(innerPane, 3.0);
        innerPane.getChildren().addAll( scaleSlider);//scaleLabel,

        getChildren().addAll(infoLabel, innerPane);
    }

    public void setInfoLabelText(String text) {
        this.infoLabel.setText(text);
    }

    public void updateSliderRange(long from, long to) {
        scaleSlider.setMin(MCMainConfiguration.MIN_SCALE_DIVIDER > from ? MCMainConfiguration.MIN_SCALE_DIVIDER : from);
        scaleSlider.setMax(MCMainConfiguration.MAX_SCALE_DIVIDER < to ? MCMainConfiguration.MAX_SCALE_DIVIDER : to);
    }


}
